package containers;

import java.util.*;
import java.util.LinkedHashMap;

public class MapPerformance {
    static List<Test<Map<Integer,Integer>>> tests = new ArrayList<Test<Map<Integer,Integer>>>();
    static {
        tests.add(new Test<Map<Integer, Integer>>("put") {
            @Override
            int test(Map<Integer, Integer> container, TestParam tp) {
                int loops = tp.loops;
                int size = tp.size;
                for (int i =0 ;i < loops;i ++){
                    container.clear();
                    for (int j =0;j < size;j ++){
                        container.put(i,j);
                    }
                }
                return loops*size;
            }
        });

        tests.add(new Test<Map<Integer, Integer>>("get") {
            @Override
            int test(Map<Integer, Integer> container, TestParam tp) {
                int loops = tp.loops;
                int span = tp.size * 2;
                for (int i = 0;i < loops;i ++){
                    for (int j = 0;j < span;j++){
                        container.get(j);
                    }
                }
                return loops*span;
            }
        });

        tests.add(new Test<Map<Integer, Integer>>("iterate") {
            @Override
            int test(Map<Integer, Integer> container, TestParam tp) {
                int loops = tp.loops * 10;
                for (int i = 0;i < loops;i ++){
                    Iterator ite = container.entrySet().iterator();
                    while (ite.hasNext()){
                        ite.next();
                    }
                }
                return loops*container.size();
            }
        });
    }

    public static void main(String[] arge){
        if (arge.length > 0)
            Tester.defaultParams = TestParam.array(arge);
        Tester.run(new TreeMap<Integer,Integer>(),tests);
        Tester.run(new HashMap<Integer,Integer>(),tests);
        Tester.run(new LinkedHashMap<Integer, Integer>(),tests);
        Tester.run(new IdentityHashMap<Integer,Integer>(),tests);
//        Tester.run(new WeakHashMap<Integer,Integer>(),tests);
        Tester.run(new Hashtable<Integer,Integer>(),tests);

    }
}
